package com.giantlizard.cloud.mybatis.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.giantlizard.cloud.base.entity.PageQuery;
import com.giantlizard.cloud.base.generics.R;
import com.giantlizard.cloud.base.json.ResponseResult;
import org.springframework.beans.BeanUtils;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用的接口层方法
 * @author kangdongwei
 * @since 2024-07-23
 * @version 1.0
 */
public abstract class BaseController<D,V> {

    /**
     * 通过id获取对象
     *
     * @param id
     * @return
     */
    public abstract R selectById(String id) throws Exception;

    /**
     * 插入一条数据
     *
     * @param dto
     * @return
     */
    public abstract R insert(D dto) throws Exception;

    /**
     * 批量创建
     *
     * @return
     * @throws Exception
     */
    public R batchInsert(List<D> dtoList) throws Exception {
        throw new NotImplementedException();
    }

    /**
     * 更新数据
     *
     * @param dto
     */
    public abstract R  update(D dto) throws Exception;

    /**
     * 更新数据
     *
     * @param dto
     */
    public abstract R  updateSelective(D dto) throws Exception;

    /**
     * 通过唯一标识删除对象
     *
     * @param id
     */
    public abstract R deleteById(String id) throws Exception;

    /**
     * 批量删除对象
     *
     * @param ids
     */
    public abstract R deleteList(List<String> ids) throws Exception;

    /**
     * 获取全部满足条件的数据
     *
     * @param dto
     * @return
     */
    public abstract R getList(D dto) throws Exception;

    /**
     * 数据分页查询
     *
     * @param dto 过滤条件
     * @param page   分页实体
     * @return
     */
    public abstract R queryByPage(D dto, PageQuery page) throws Exception;

    public abstract V transForToVo(D var1);

    public List<V> transForToVo(List<D> dtoList) {
        if (dtoList != null && dtoList.size() != 0) {
            List<V> voList = new ArrayList();
            dtoList.forEach((o) -> {
                voList.add(this.transForToVo(o));
            });
            return voList;
        } else {
            return new ArrayList();
        }
    }


    public IPage<V> transForToVo(IPage<D> dtoPage) {
        if (dtoPage != null && dtoPage.getRecords() != null && dtoPage.getRecords().size() != 0) {
            IPage<V> voPage = new Page<>();
            BeanUtils.copyProperties(dtoPage, voPage);
            if (dtoPage.getTotal() <= 0L) {
                return voPage;
            } else {
                List<V> voList = new ArrayList<>();
                dtoPage.getRecords().forEach((o) -> {
                    voList.add(this.transForToVo(o));
                });
                voPage.setRecords(voList);
                return voPage;
            }
        } else {
            return new Page<>();
        }
    }
}
